//
//  String+Ext.swift
//  yiplayer
//
//  Created by cgw on 2024/5/10.
//

import Foundation

import CommonCrypto
extension String {
    var md5:String {
        let utf8 = cString(using: .utf8)
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        CC_MD5(utf8, CC_LONG(utf8!.count - 1), &digest)
        return digest.reduce("") { $0 + String(format:"%02x", $1) }
    }
    
    static func formatObject(obj: Any?) -> String? {
        
        if let obj = obj {
            if obj is String {
                if (obj as! String) == "NULL" {
                    return nil;
                }
                return obj as? String
            }
            else if obj is Int {
                let num = obj as! Int
                return String(num)
            }
        }
        
        return nil
    }
}


extension Int{
    static func formatObject(_ obj: Any?) -> Int? {
        
        if let ob = obj as? String {
            if ob == "NULL" {
                return nil
            }
            return Int(ob)
        }
        
        else if let ob = obj as? Int {
            return ob
        }
        
        return nil
    }
}
